<?php  
namespace app\common\services;

use yii\helpers\Url;
use app\common\services\UtilService;
use app\common\services\BaseService;
class UploadService extends BaseService{
	public static function uploadByFile($file_name,$file_path,$bucket=''){

	//判断$file_name $file_path $bucket 
	if(!$file_name){
		return self::_err("请输入合法文件名");
	}
	if(!$file_path){
		return self::_err("请输入合法file_path");
	}
	$upload_config=\Yii::$app->params['upload'];
	if(!isset($upload_config[$bucket])){
		return self::_err("指定参数bucket错误");
	}
	$tmp_file_extend=explode(".",$file_name);
	$file_extend=end($tmp_file_extend);
	$hash_key=md5(file_get_contents($file_path));
	//在每个篮子下面按日期存放图片
	$date=date('y-m-d');
	$path=UtilService::getRootPath().'/web'.$upload_config[$bucket].'/';
	$upload_dir=$path.$date;
	if(!file_exists($upload_dir)){
		mkdir($upload_dir,0777);
		chmod($upload_dir,0777);
	}
	$upload_full_name=$hash_key.'.'.$file_extend;
	// echo $date.'/'.$upload_full_name;
	if(is_uploaded_file($file_path)){move_uploaded_file($file_path,$upload_dir.'/'.$upload_full_name);
	}else{
		file_put_contents($upload_dir.'/'.$upload_full_name, file_get_contents($file_path));
	}
	return [
		'code'=>200,
		'path'=>$date.'/'.$upload_full_name,
		'prefix'=>$upload_config[$bucket]
	];
	}
}
?>